package uniandes.comit.OIServer.mundo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class GestorSeguridad 
{
	/**
	 * Cipher a utilizar
	 */
	 private Cipher aesC;
	 /**
	  * llave a utilizar para encriptar y desencriptar 
	  */
	 private SecretKey secretKey;
	
	 /**
	  * contructor
	  */
	public GestorSeguridad(){
		try{
			
			KeyGenerator keyGen = KeyGenerator.getInstance("AES");
			keyGen.init(128);
			secretKey = keyGen.generateKey();
			aesC = Cipher.getInstance("AES");
			}
			
			catch (NoSuchAlgorithmException noSuchAlgo)
			{
				System.out.println(" No Such Algorithm exists " + noSuchAlgo);
			}
			
				catch (NoSuchPaddingException noSuchPad)
				{
					System.out.println(" No Such Padding exists " + noSuchPad);
				}
			
					
	}
	
	/**
	 * retorna la llave para desencriptar
	 * @return
	 */
	public SecretKey getSecretKey() {
		return secretKey;
	}

	/**
	 * metodo q encripta
	 * @param stringAEncriptar
	 * @return
	 */
	public String encriptar(String stringAEncriptar){
		
		
		
		try {
			aesC.init(Cipher.ENCRYPT_MODE,secretKey);
		} catch (InvalidKeyException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String strCipherText = new String();
		try{
		
		byte[] byteDataToEncrypt = stringAEncriptar.getBytes();
		byte[] byteCipherText = aesC.doFinal(byteDataToEncrypt); 
		strCipherText = new BASE64Encoder().encode(byteCipherText);
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		return strCipherText;
	}
	
	/**
	 * metodo q desencripta(se le entrega la llave de desencripcion)
	 * @param textoEncriptado
	 * @param sKey
	 * @return
	 */
	public String desencriptar(String textoEncriptado, SecretKey sKey){
		
		
		String texto = null;
        try{
        	byte[] byteCipherText=new BASE64Decoder().decodeBuffer(textoEncriptado);

    		aesC.init(Cipher.DECRYPT_MODE,sKey,aesC.getParameters());
    		byte[] byteDecryptedText = aesC.doFinal(byteCipherText);
    		texto = new String(byteDecryptedText);    		
        }
        catch (Exception e) {
        	e.printStackTrace();
        }

		return texto;
	}
}
